package com.midcent.networklib.interfac;

import android.util.Log;

import com.midcent.networklib.manager.HttpRequestNetManager;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.logging.Logger;

/**
 * Created by kexiang on 16/7/25.
 */
public class RequestUtils {

    /**
     * String 计算出Sha256
     *
     * @param srcString 数据源kekk===pAGCX2Uh
     * @return
     */

    private static final String key = "pAGCX2Uh";

    public static String sha256(String srcString) {
        if (srcString == null)
            srcString = "";
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            return srcString;
        }
        byte[] digest = md.digest(srcString.getBytes());
        StringBuffer digestHexStrBf = new StringBuffer();
        for (int i = 0; i < digest.length; i++) {
            digestHexStrBf.append(hex(digest[i]));
        }
        return digestHexStrBf.toString();
    }

    /**
     * String 计算出Md5
     *
     * @param srcString 数据源
     * @return
     */
    public static String sha1(String srcString) {
        if (srcString == null)
            srcString = "";
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            return srcString;
        }
        byte[] digest = md.digest(srcString.getBytes());
        StringBuffer digestHexStrBf = new StringBuffer();
        for (int i = 0; i < digest.length; i++) {
            digestHexStrBf.append(hex(digest[i]));
        }
        return digestHexStrBf.toString();
    }

    /**
     * 将byte转换为hex字符串
     *
     * @param ib
     * @return
     */
    private static String hex(byte ib) {

        String s = Integer.toHexString((int) ib & 0xff);
        if (s.length() == 1) {
            return "0" + s;
        } else {
            return s;
        }
    }

    public static String getSignature(String key, String... args) {
        try {
            args = Arrays.copyOf(args, args.length + 1);
            args[args.length - 1] = key;
            StringBuffer sb = new StringBuffer();
            // 字符串排序
            Arrays.sort(args);
            for (int i = 0; i < args.length; i++) {
                sb.append(args[i]);
            }
            // SHA1签名生成
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(sb.toString().getBytes());
            byte[] digest = md.digest();

            StringBuffer hexstr = new StringBuffer();
            String shaHex = "";
            for (int i = 0; i < digest.length; i++) {
                shaHex = Integer.toHexString(digest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexstr.append(0);
                }
                hexstr.append(shaHex);
            }
            return hexstr.toString();
        } catch (Exception e) {
            throw new RuntimeException("计算hash值失败", e);
        }
    }

    public static String getPaytoken(String pass, String timestamp) {
        return getSignature(key, HttpRequestNetManager.deviceno, sha256(pass), timestamp);
    }

    public static String getGamePaytoken(String loginname, String pass, String timestamp,String appCode)
    {
        return getSignature(key,loginname,appCode,sha256(pass),timestamp);
    }
    public static String getPaytoken(String loginname, String pass, String timestamp) {
        return getSignature(key, loginname, sha256(pass), timestamp);
    }

    public static String getRedPacketPaytoken(String userid, String sendamount, String message, String paytype, String pass, String timestamp) {
        return getSignature(key, userid, sendamount, message, paytype, sha256(pass), timestamp);
    }

    public static String getSdkPayToken(String sign, String loginname, String pass, String timestamp) {
        return getSignature(key, sign, loginname, sha256(pass), timestamp);
    }

    public static String getsign(String key, String appid, String orderno, String amount, String itemname, String notifyurl) {
        return getSignature(key, appid, orderno, amount, itemname, notifyurl);
    }
}


